<template>
  <div>
    <a href="javascript:void(0);" class="address" @click="addressFlag = true"
      ><i class="el-icon-plus"></i>新增收货地址</a
    >

    <!-- 新增地址弹窗 -->
    <el-dialog
      title="新增收货地址"
      :visible.sync="addressFlag"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="addressModel"
        label-width="80px"
      >
        <el-form-item label="收货人" prop="consignee">
          <el-input v-model="addressModel.consignee"></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input
            v-model="addressModel.phone"
            maxlength="11"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="邮政编码" prop="postcode">
          <el-input v-model="addressModel.postcode"></el-input>
        </el-form-item>
        <el-form-item label="选择区域">
          <el-col :span="8">
            <el-input
              placeholder="省"
              v-model="addressModel.province"
            ></el-input>
          </el-col>
          <el-col :span="8">
            <el-input placeholder="市" v-model="addressModel.city"></el-input>
          </el-col>
          <el-col :span="8">
            <el-input
              placeholder="区/县"
              v-model="addressModel.county"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input
            type="textarea"
            placeholder="请输入详细地址"
            maxlength="150"
            show-word-limit
            :autosize="{ minRows: 4, maxRows: 8 }"
            v-model="addressModel.detailAddress"
          ></el-input>
        </el-form-item>
        <el-form-item label="地址标签">
          <a href="javascript:void(0);" class="tags">学校</a>
          <a href="javascript:void(0);" class="tags">公司</a>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="addAddress">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      addressFlag: false,
      addressModel: {
        consignee: '',
        phone: '',
        postcode: '',
        province: '',
        city: '',
        county: '',
        detailAddress: ''
      },
      rules: {
        consignee: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        phone: [{ required: true, message: '请输入手机号码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // X号关闭弹窗
    handleClose() {
      // 关闭弹窗
      this.addressFlag = false
      // 清除条件校验
      this.$refs.form.resetFields()
    },
    // 提交添加地址
    addAddress() {
      this.$refs.form.validate((valid) => {
        if (valid) {
            this.$message.success(JSON.stringify(this.addressModel))
            this.handleClose()
        } else {
            this.$message.warning('添加失败')
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.address {
  font-size: 14px;
  color: #277ccc;
  cursor: pointer;
}
.tags {
  border: 1px solid #dcdfe6;
  padding: 7px 15px;
  border-radius: 4px;
  font-size: 12px;
  margin-right: 5px;
}
</style>
